package com.qqzone.service.impl;

import com.qqzone.dao.TopicDAO;
import com.qqzone.pojo.Reply;
import com.qqzone.pojo.Topic;
import com.qqzone.pojo.UserBasic;
import com.qqzone.service.ReplyService;
import com.qqzone.service.TopicService;
import com.qqzone.service.UserBasicService;

import java.util.List;

/**
 * @author Yang
 * @creat 11:22 2022/8/25
 * @sice 1.8
 */

public class TopicServiceImpl implements TopicService {

    private TopicDAO topicDAO = null;

    private ReplyService replyService = null;

    private UserBasicService userBasicService = null;

    @Override
    public List<Topic> getTopicList(UserBasic userBasic) {
        return topicDAO.getTopicList(userBasic);
    }

    @Override
    public Topic getTopicById(Integer id) {
        // 根据id获取指定的topic信息，包含这个topic关联的作者信息
        Topic topic = topicDAO.getTopic(id);

        UserBasic author = topic.getAuthor();
        author = userBasicService.getUserBasicById(author.getId());
        topic.setAuthor(author);

        // 得在 replyService.getReplyByTopicId 中完成 replyList 中每一个的 reply 的初始化
        // 其中包括 hostReply
        List<Reply> replyList = replyService.getReplyByTopicId(topic.getId());
        topic.setReplyList(replyList);

        return topic;
    }

    @Override
    public void delTopic(Integer id) {
        Topic topic = topicDAO.getTopic(id);
            if (topic != null) {
            replyService.delReplyList(topic);

            topicDAO.delTopic(topic);
        }
    }

    @Override
    public List<Topic> getTopicListByUserId(Integer userId) {
        List<Topic> topicList = topicDAO.getTopicList(new UserBasic(userId));
        return topicList;
    }

    @Override
    public void addTopic(Topic topic) {
        topicDAO.addTopic(topic);
    }
}
